package com.yiban.demo.LoginUser.Service.ServiceImp;

import com.yiban.demo.LoginUser.DAO.LoginUserMapper;
import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import com.yiban.demo.LoginUser.Entity.LoginUserTransfer;
import com.yiban.demo.LoginUser.Entity.LoginUserVO;
import com.yiban.demo.LoginUser.Entity.ROLE;
import com.yiban.demo.LoginUser.Service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.UUID;

//服务的具体实现的命名规范是xxServiceImp
//service层对参数进行处理，比如，核验，转化，同时使用多个mapper
@Service
public class LoginUserServiceImp implements LoginUserService {
    private static final  String REGISTER_MAIL_CODE_PREFIX = "register";

    //bean相同类属性互相拷贝，这个的实现基于字节码较为高效

    private static final char[] CHARS= {1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    @Autowired
    private LoginUserMapper loginUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean register(LoginUserPO loginUserPO) {
        if (loginUserMapper.selectOneByUsername(loginUserPO.getUsername())!=null) {
            return false;
        }
        if (loginUserPO.getNickname() == null) {
            loginUserPO.setNickname("njupt"+ UUID.randomUUID());
        }
        loginUserPO.setRole(ROLE.USER.getName());
        return loginUserMapper.insertOne(loginUserPO);
    }

    @Override
    public LoginUserPO loginIn(LoginUserVO loginUserVO) {
        final LoginUserPO userPO = loginUserMapper.selectOneByUsername(loginUserVO.getUsername());
        if (userPO != null && userPO.getPassword().equals(loginUserVO.getPassword())){
            return userPO;
        }
        return null;
    }


    @Override
    public boolean modifyPassword(LoginUserPO nowUser,String oldPassword,String newPassword) {
        if (nowUser.getPassword().equals(oldPassword)) {
            return loginUserMapper.updatePasswordById(nowUser.getUserId(), newPassword);
        }
        return false;
    }

    @Override
    public boolean sentMailCode(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.isBlank()){
            return false;
        }
        //todo 发短信
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            codeBuilder.append(CHARS[((int) (CHARS.length * Math.random()))]);
        }
        final ValueOperations ops = redisTemplate.opsForValue();
        String keyName = REGISTER_MAIL_CODE_PREFIX + phoneNumber;
        if (ops.get(keyName) == null) {
            ops.set(keyName, codeBuilder.toString(), Duration.ofSeconds(300));
        }
        return true;
    }



    @Override
    public boolean checkMailCode(String code, String phoneNumber) {
        String mailCode = (String) redisTemplate.opsForValue().get(REGISTER_MAIL_CODE_PREFIX + phoneNumber);
        if (code.equals(mailCode)) {
            redisTemplate.delete(REGISTER_MAIL_CODE_PREFIX + phoneNumber);
            return true;
        }
        return false;
    }

    @Override
    public LoginUserPO transToLoginPo(LoginUserVO loginUserVO) {

        return LoginUserTransfer.transToLoginPo(loginUserVO);
    }
    @Override
    public LoginUserVO transToLoginVo(LoginUserPO loginUserPO) {
        return LoginUserTransfer.transToLoginVo(loginUserPO);
    }
}
